import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/event_or_delegate/reload_event.dart';
import 'package:flutter_app_unionad/model/partner_ship_info_model.dart';
import 'package:flutter_app_unionad/request/partner_ship_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_height_widget.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/ui/partnership/facility_list_page.dart';
import 'package:flutter_app_unionad/ui/partnership/join_partnership_page.dart';
import 'package:flutter_app_unionad/ui/partnership/widget/add_facility_widget.dart';
import 'package:flutter_app_unionad/ui/partnership/widget/partnership_widget.dart';
import 'package:flutter_app_unionad/ui/partnership/widget/un_partnership_widget.dart';
import 'package:flutter_app_unionad/view_model/change_device_event.dart';

class PartnershipPage extends StatefulWidget {
  @override
  _PartnershipPageState createState() => _PartnershipPageState();
}

//
class _PartnershipPageState extends State<PartnershipPage> with AutomaticKeepAliveClientMixin, PartnershipPageDelegate {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  bool isPartnership = true;
  PartnerShipInfoModel? infoModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (Configuration().phone.stringIsEmpty) {
      PartnerShipRequest.getConfigBykey(1).then((value) {
        if (value != null && mounted) {
          setState(() {
            Configuration().phone = value;
          });
        }
      });
    }
    PartnerShipRequest.getConfigBykey(0).then((value) {
      if (value != null && mounted) {
        setState(() {
          Configuration().maxCount = value;
        });
      }
    });
    PartnerShipRequest.getConfigBykey(2).then((value) {
      print('000000  $value');
      if (value != null && mounted) {
        setState(() {
          Configuration().price = value;
        });
      }
    });
    getData();
  }

  void getData() {
    PartnerShipRequest.getPartnerShipRequest().then((value) {
      if (mounted)
        setState(() {
          infoModel = value;
        });
    });
  }

  @override
  // ignore: must_call_super
  Widget build(BuildContext context) {
    isPartnership = true;
    return BaseNavigationHeightWidget(
      navHeight: isPartnership ? 256.px : 220.px,
      needScrollView: false,
      child: buildContent(),
    );
  }

  Widget buildContent() {
    if (infoModel == null ||
        Configuration().phone.stringIsEmpty ||
        Configuration().maxCount.stringIsEmpty ||
        Configuration().price.stringIsEmpty) {
      return ProgressDialog.showFirstLoading();
    }
    return infoModel!.status == 1
        ? PartnerShipWidget(
            delegate: this,
          )
        : UnPartnershipWidget(
            delegate: this,
          );
  }

  @override
  void addFacility() {
    //新增设备
  }

  @override
  void becomePartnership() {
    //成为合伙人
    NavigatorUtil.push(context, JoinPartnershipPage.routeName).then((value) {
      if (value != null && value) {
        getData();
      }
    });
  }

  @override
  void facilityList() {
    NavigatorUtil.push(context, FacilityListPage.routeName);
  }

  @override
  void pointMoney() {
    // TODO: implement pointMoney
  }

  @override
  void withdraw() {
    // TODO: implement withdraw
  }
}

abstract class PartnershipPageDelegate {
  void becomePartnership();

  void withdraw();

  void addFacility();

  void facilityList();

  void pointMoney();
}
